from PySide6.QtCore import QSize, Qt
from PySide6.QtWidgets import QApplication, QMessageBox, QPushButton, QVBoxLayout, QWidget
import sys

class CustomWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()

        self.setWindowTitle('QMessage 示例')

        btn_hard = QPushButton('Hard 不推荐的手动初始化模式[但是弹出框是全屏居中的]')
        btn_hard.clicked.connect(self.btn_hard_clicked)

        btn_critical = QPushButton('critical 严重错误[窗口居中]')
        btn_critical.clicked.connect(self.btn_critical_clicked)

        btn_question = QPushButton('question')
        btn_question.clicked.connect(self.btn_question_clicked)

        btn_information = QPushButton('information')
        btn_information.clicked.connect(self.btn_information_clicked)

        btn_warning = QPushButton('warning')
        btn_warning.clicked.connect(self.btn_warning_clicked)

        btn_about = QPushButton('about')
        btn_about.clicked.connect(self.btn_about_clicked)

        layout = QVBoxLayout()
        layout.addWidget(btn_hard)
        layout.addWidget(btn_critical)
        layout.addWidget(btn_question)
        layout.addWidget(btn_information)
        layout.addWidget(btn_warning)
        layout.addWidget(btn_about)
        self.setLayout(layout)


    def btn_hard_clicked(self):
        # 消息弹框配置方式1（不推荐使用）
        message = QMessageBox()
        # message.setMinimumSize(700, 200)  # 这个最小尺寸不生效
        message.setWindowTitle('消息标题')
        message.setText('发生了一些事【Text】')
        message.setInformativeText('你想知道有关它的事情吗?【InformativeText】')
        message.setIcon(QMessageBox.Critical)
        message.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel) # 设置可选按键
        message.setDefaultButton(QMessageBox.Ok)   # 默认按键行为配置    
        ret = message.exec()    # 获取选择结果
        if ret == QMessageBox.Ok:
            print('btn_hard: 点击确认')
        else:
            print('btn_hard: 点击取消')

    def btn_critical_clicked(self):
        # 消息弹框配置方式2（推荐）
        ret = QMessageBox.critical(self, "消息标题【Title】", "Critical消息【Text】",
                                   QMessageBox.Ok | QMessageBox.Cancel)
        if ret == QMessageBox.Ok:
            print('btn_critical: 点击确认')
        else:
            print('btn_critical: 点击取消')

    def btn_question_clicked(self):
        ret = QMessageBox.question(self, "消息标题【Title】", "Critical消息【Text】",
                                   QMessageBox.Ok | QMessageBox.Cancel)
        if ret == QMessageBox.Ok:
            print('点击确认')
        else:
            print('点击取消')

    def btn_information_clicked(self):
        ret = QMessageBox.information(self, "消息标题【Title】", "Critical消息【Text】",
                                   QMessageBox.Ok | QMessageBox.Cancel)
        if ret == QMessageBox.Ok:
            print('点击确认')
        else:
            print('点击取消')

    def btn_warning_clicked(self):
        ret = QMessageBox.warning(self, "消息标题【Title】", "Critical消息【Text】",
                                   QMessageBox.Ok | QMessageBox.Cancel)
        if ret == QMessageBox.Ok:
            print('点击确认')
        else:
            print('点击取消')

    def btn_about_clicked(self):
        # 注意 about 模式是没有按键选择的
        ret = QMessageBox.about(self, "消息标题【Title】", "Critical消息【Text】")
        if ret == QMessageBox.Ok:
            print('点击确认')
        else:
            print('点击取消')


app = QApplication(sys.argv)

window = CustomWidget()
window.show()

app.exec()